package com.itheima.grow.util;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 阿里云短信3.6.x工具类
 */
@Slf4j
public class SendSmsUtil {
    // 签名
    private static final String signName = "上汽大众";

    // 模板
    private static final String templateCode = "";

    // 阿里云短信配置信息
    private static final String accessKeyId = "LTAI5tAJU4uE7rWFVGgrCEMK";
    private static final String accessKeySecret = "LQKypYDhF7eE96oF5058ZoPkxJnMqL";
    private static final String REGION_ID = "cn-hangzhou";
    private static final String PRODUCT = "Dysmsapi";
    private static final String DOMAIN = "dysmsapi.aliyuncs.com";

    /**
     *
     * @return
     */
    public static void sendSMS() {
        Map<String,Object> params=new HashMap<>();
        params.put("user_name","谢庭胜");
        params.put("process_by","张大大");
        params.put("phone","17379737822");
        try {
            IClientProfile profile = DefaultProfile.getProfile(REGION_ID, accessKeyId, accessKeySecret);
            DefaultProfile.addEndpoint(REGION_ID, REGION_ID, PRODUCT, DOMAIN);
            //Step1 得到阿里云发送短信client
            IAcsClient acsClient = new DefaultAcsClient(profile);
            //Step2 构建请求
            SendSmsRequest request = new SendSmsRequest();
            //Step3 设置请求参数
            request.setMethod(MethodType.POST);
            //手机号
            request.putQueryParameter("PhoneNumbers","15779587011");
            //短信签名
            request.putQueryParameter("SignName", "上汽大众");
            //短信模板号
            request.putQueryParameter("TemplateCode", "SMS_464065536");
            //传入参数
            request.putQueryParameter("TemplateParam", JSONObject.toJSONString(params));
            //Step4 发送短信得到响应对象
            SendSmsResponse response = acsClient.getAcsResponse(request);
            log.info("发送短信响应：{}",JSONObject.toJSONString(response));
        } catch (ClientException e) {
            log.error("发送短信失败!", e);
        } finally {
            //Step5 保存发送短信记录
        }
    }

    public static void main(String[] args) {
        sendSMS();
    }
}
